package com.skirmisher.inspection.conform.service.impl;

import com.skirmisher.inspection.common.entity.InsLoop;
import com.skirmisher.inspection.common.entity.InsLoopSave;
import com.skirmisher.inspection.common.mapper.InsLoopMapper;
import com.skirmisher.inspection.common.service.SystemCodeCacheService;
import com.skirmisher.inspection.conform.entity.*;
import com.skirmisher.inspection.conform.mapper.InsConformTemplateItemsMapper;
import com.skirmisher.inspection.conform.mapper.InsConformTemplateMapper;
import com.skirmisher.inspection.conform.mapper.InsConformWorkerMapper;
import com.skirmisher.inspection.conform.service.InsConformTemplateService;
import com.skirmisher.inspection.daily.entity.InsDailyTemplate;
import com.skirmisher.inspection.daily.entity.InsDaliyTemplateItems;
import com.skirmisher.inspection.daily.entity.InsDaliyWorker;
import com.skirmisher.inspection.inspection.testController.ResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName InsConformTemplateServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/10 下午8:51
 * @Version 1.0
 */
@Service
@Transactional
@Slf4j
public class InsConformTemplateServiceImpl implements InsConformTemplateService {

    @Autowired
    private SystemCodeCacheService systemCodeCacheService;
    @Autowired
    private InsConformTemplateMapper insConformTemplateMapper;
    @Autowired
    private InsConformWorkerMapper insConformWorkerMapper;
    @Autowired
    private InsLoopMapper insLoopMapper;
    @Autowired
    private InsConformTemplateItemsMapper insConformTemplateItemsMapper;

    @Override
    public void saveConformTemp(InsConformTemplateSave tempSave, ResponseModel responseModel) {

        //处理主单据数据
        InsConformTemplate insConformTemplate = new InsConformTemplate();
        String tempId = UUID.randomUUID().toString();
        if(!StringUtils.isEmpty(tempSave.getId())){
            tempId = tempSave.getId();
        }
        BeanUtils.copyProperties(tempSave, insConformTemplate);
        //填写其他参数
        insConformTemplate.setId(tempId);
        //获取模板编码值
        String nextCode = systemCodeCacheService.getNextCode(SystemCodeCacheService.CONFORMTEMP);
        insConformTemplate.setTempCode(nextCode);
        Date now = new Date();
        insConformTemplate.setLastUpdateTime(now);
        insConformTemplate.setLastUpdateWorkerCode(tempSave.getWorkCode());
        insConformTemplate.setLastUpdateWorkerName(tempSave.getWorkName());
        insConformTemplate.setWorkerBindType("temp");
        insConformTemplate.setRecTime(now);
        insConformTemplate.setTempStatus("0");
        insConformTemplate.setRecCode(tempSave.getWorkCode());
        insConformTemplate.setRecName(tempSave.getWorkName());

        insConformTemplateMapper.insert(insConformTemplate);
        //处理巡查项目数据
        List<InsConformTemplateItems> items = tempSave.getItems();
        if (items != null){
            for (InsConformTemplateItems item : items) {
                item.setItemId(UUID.randomUUID().toString());
                item.setTempId(tempId);
                if (StringUtils.isEmpty(item.getItemCode())){
                    String noDateCode = systemCodeCacheService.getNoDateCode(SystemCodeCacheService.ITEMCODE);
                    item.setItemCode(noDateCode);
                }

                insConformTemplateItemsMapper.insert(item);
            }
        }


        //处理周期数据
        List<InsLoopSave> loops = tempSave.getLoops();
        for (InsLoopSave loopSave : loops) {
            InsLoop loop = new InsLoop();
            BeanUtils.copyProperties(loopSave, loop);
            loop.setLoopId(UUID.randomUUID().toString());
            loop.setTempId(tempId);
            loop.setLoopName(insConformTemplate.getTempName());

            insLoopMapper.insert(loop);
        }

        //处理人员数据
        List<InsConformWorker> workers = tempSave.getWorkers();
        for (InsConformWorker worker : workers) {
            worker.setId(UUID.randomUUID().toString());
            worker.setBindId(tempId);
            worker.setTempId(tempId);
            insConformWorkerMapper.insert(worker);
        }
    }

    @Override
    public InsConformTemplate getTempDetail(String tempId) {
        InsConformTemplate insConformTemplate = insConformTemplateMapper.getById(tempId);
        if (insConformTemplate == null){
            return null;
        }
        List<InsConformTemplateItems> items = insConformTemplateItemsMapper.getByTempId(tempId);
        insConformTemplate.setItems(items);
        List<InsLoop> loops = insLoopMapper.getByTempId(tempId);
        insConformTemplate.setLoops(loops);
        List<InsConformWorker> workers = insConformWorkerMapper.getByBindId(tempId);
        insConformTemplate.setWorkers(workers);
        return insConformTemplate;

    }

    @Override
    public void getTempPageData(InsConformTempQueryEntity query) {

        List<Object> dataList = insConformTemplateMapper.pageQueryData(query);
        int count = insConformTemplateMapper.pageQueryCount(query);

        query.setDateList(dataList);
        query.setCount(count);
    }

    @Override
    public void deletConformTemp(List<String> tempIds) {

        for (String tempId : tempIds) {
            InsConformTemplate insConformTemplate = new InsConformTemplate();
            insConformTemplate.setId(tempId);
            insConformTemplate.setTempStatus("99");
            insConformTemplateMapper.updateIgnoreNull(insConformTemplate);
        }

    }

    @Override
    public void updateConformTemp(InsConformTemplateSave tempSave, ResponseModel responseModel) {

        //删除日常巡查模板 内容 人员 周期
        String tempID = tempSave.getId();
        insConformTemplateMapper.deleteTemp(tempID);
        insConformTemplateItemsMapper.deleteTempItem(tempID);
        insConformWorkerMapper.deleteTempWorker(tempID);
        insLoopMapper.deleteTempLoop(tempID);

        saveConformTemp(tempSave,responseModel);
    }

    @Override
    public void startConformTemp(List<String> tempIds) {
        for (String tempId : tempIds) {
            InsConformTemplate insConformTemplate = new InsConformTemplate();
            insConformTemplate.setId(tempId);
            insConformTemplate.setTempStatus("1");
            insConformTemplateMapper.updateIgnoreNull(insConformTemplate);
        }
    }
}
